package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.manager.dto.MenuDto;
import com.kelvem.saas.workbanch.core.manager.model.BizPageEntity;
import com.kelvem.saas.workbanch.core.manager.request.BizPageQueryVo;
import com.kelvem.saas.workbanch.core.manager.service.BizPageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 业务页面控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/bizPage")
public class BizPageController {

    @Resource
    private BizPageService bizPageService;
    
    /**
     * 增加业务页面
     * curl -X POST http://localhost:9090/bizPage/v1/add -H 'Content-Type: application/json' -d '{"id": null, "menuName": "menu_name", "menu1": "menu1", "menu2": "menu2", "pageType": "page_type", "templateType": "template_type", "templateId": 3, "metaTableId": 3, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param bizPage 业务页面信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) BizPageEntity bizPage) {
        SaasAssert.notNull(bizPage);

        log.debug("BizPageController:add:start, bizPage=【{}】", bizPage);
        Long bizPageId = bizPageService.addBizPage(bizPage);
        log.debug("BizPageController:add:end, bizPageId={}", bizPageId);

        return bizPageId;
    }

    /**
     * 修改业务页面
     * curl -X PUT http://localhost:9090/bizPage/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "menuName": "menu_name", "menu1": "menu1", "menu2": "menu2", "pageType": "page_type", "templateType": "template_type", "templateId": 3, "metaTableId": 3, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param bizPage 业务页面信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) BizPageEntity bizPage) {
        SaasAssert.notNull(bizPage);

        log.debug("BizPageController:update:start, bizPage=【{}】", bizPage);
        int count = bizPageService.updateBizPage(bizPage);
        log.debug("BizPageController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除业务页面
     * curl -X DELETE 'http://localhost:9090/bizPage/v1/delete/10002'
     *
     * @param bizPageId 业务页面id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{bizPageId}")
    public Integer delete(@PathVariable(required = false) Long bizPageId) {
        SaasAssert.notNull(bizPageId);

        log.debug("BizPageController:delete:start, bizPageId={}", bizPageId);
        int count = bizPageService.deleteBizPage(bizPageId);
        log.debug("BizPageController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询业务页面分页数据
     * curl -X POST http://localhost:9090/bizPage/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param bizPageQueryVo 查询用
     * @return Page<BizPageEntity> 业务页面分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<BizPageEntity> queryPage(@RequestBody(required = false) BizPageQueryVo bizPageQueryVo) {
        SaasAssert.notNull(bizPageQueryVo);

        log.debug("BizPageController:queryPage:start, bizPageQueryVo=【{}】", bizPageQueryVo);
        Page<BizPageEntity> page = bizPageService.queryBizPagePage(bizPageQueryVo);
        log.debug("BizPageController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查业务页面信息
     * curl -X GET 'http://localhost:9090/bizPage/v1/get/10000'
     *
     * @param bizPageId 业务页面id
     * @return BizPage 业务页面信息
     */
    @GetMapping("/v1/get/{bizPageId}")
    public BizPageEntity getById(@PathVariable(required = false) Long bizPageId) {
        SaasAssert.notNull(bizPageId);

        log.debug("BizPageController:getById:start, bizPageId={}", bizPageId);
        BizPageEntity bizPage = bizPageService.getBizPageById(bizPageId);
        log.debug("BizPageController:getById:end, bizPage=【{}】", bizPage);

        return bizPage;
    }

    /**
     * 保存业务页面
     * curl -X POST http://localhost:9090/bizPage/v1/save -H 'Content-Type: application/json' -d '{"id": null, "menuName": "menu_name", "menu1": "menu1", "menu2": "menu2", "pageType": "page_type", "templateType": "template_type", "templateId": 3, "metaTableId": 3, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param bizPage 业务页面信息
     * @return Long 主键id
     */
    @PostMapping("/v1/save")
    public Long save(@RequestBody(required = false) BizPageEntity bizPage) {
        SaasAssert.notNull(bizPage);

        log.debug("BizPageController:save:start, bizPage=【{}】", bizPage);
        Long bizPageId = bizPageService.saveBizPage(bizPage);
        log.debug("BizPageController:save:end, bizPageId={}", bizPageId);

        return bizPageId;
    }

    /**
     * 根据token查询菜单列表
     * curl http://localhost:9090/bizPage/v1/queryMenuByToken -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImRpc3BsYXlOYW1lIjoi6LaF57qn5peg5pWM5aSp6LSiIiwiYXZhdGFyIjoiaHR0cHM6Ly93cGltZy53YWxsc3Rjbi5jb20vZjc3ODczOGMtZTRmOC00ODcwLWI2MzQtNTY3MDNiNGFjYWZlLmdpZiIsImV4cCI6MTcyMTE5NzA2MCwiaWF0IjoxNzIxMTYxMDYwLCJvcmdJZCI6MX0.-EoiyPlekuIeXFtQXdsuZXaQwnZaaMs1Tizonu4e-_U'
     *
     * @return 菜单列表
     */
    @GetMapping("/v1/queryMenuByToken")
    public List<MenuDto> queryMenuByToken() {
        log.debug("BizPageController:queryMenuByToken:start");
        List<MenuDto> list = this.bizPageService.queryMenuByToken();
        log.debug("BizPageController:queryMenuByToken:end, list.size={}", list.size());

        return list;
    }
}
